
(function () {
    'use strict';

    angular.module('KingAdmin.pages.common')
        .controller('VideoCtrl', VideoCtrl);

    /** @ngInject */
    function VideoCtrl($scope, $state,$uibModalInstance, cameraId, CameraService) {

        console.log("ng modal");
        $scope.title = '测试摄像头';
        $scope.ratio = 'HD';

        $uibModalInstance.rendered.then(function() {
            // Get camera info
            // 检查插件是否已经安装过
            var iRet = WebVideoCtrl.I_CheckPluginInstall();
            if (-2 == iRet) {
                // console.log("您的Chrome浏览器版本过高，不支持NPAPI插件！");
                return;
            } else if (-1 == iRet) {
                // alert("您还未安装过插件，双击开发包目录里的WebComponentsKit.exe安装！");
                // return;
            }


            console.log({id: cameraId});

            CameraService.getInfo({id:cameraId},
                function (data) {
                    console.log(data);
                    console.log('rendered----');
                    $scope.camera = data;

                    $scope.ipcLogin($scope.camera);
                });

            console.log('rendered')
        });

        $uibModalInstance.closed.then(function() {
            console.log('closed');
            $scope.ipcLogout($scope.camera);
        });

        $scope.up = function () {
            console.log('up');
            $scope.mouseDownPTZControl(1);
        };

        $scope.down = function () {
            console.log('down');
            $scope.mouseDownPTZControl(2);

        };

        $scope.left = function () {
            console.log('left');
            $scope.mouseDownPTZControl(3);

        };

        $scope.right = function () {
            console.log('right');
            $scope.mouseDownPTZControl(4);

        };

        $scope.zoomIn = function () {
            $scope.PTZZoomIn();
            console.log('zoomIn');
        };

        $scope.zoomOut = function () {
            $scope.PTZZoomout();
            console.log('zoomOut');
        };

        $scope.hdVideo = function () {
            if ($scope.ratio == 'HD') {
                $scope.ratio = 'SD';
                $scope.changeHDView(true);
            } else {
                $scope.ratio = 'HD';
                $scope.changeHDView(false);
            }

            console.log('hdVideo');
        };

        $scope.controlUp = function() {
            console.log('up');
            $scope.mouseUpPTZControl();
        };

        $scope.zoomUp = function() {
            $scope.PTZZoomStop();
        };


        // 初始化插件
// 全局保存当前选中窗口
        $scope.g_iWndIndex = 0; //可以不用设置这个变量，有窗口参数的接口中，不用传值，开发包会默认使用当前选择窗口
        $scope.gSzIp = '';

// 登录
        $scope.ipcLogin = function (camera) {
            console.log('ipclogin---');
            console.log(camera);
            var szIP = camera['ip'],
                szPort = camera['port'],
                szUsername = camera['user'],
                szPassword = camera['pass'];

            $scope.gSzIp = szIP;

            // 初始化插件参数及插入插件
            WebVideoCtrl.I_InitPlugin(900, 510, {
                bWndFull: true,//是否支持单窗口双击全屏，默认支持 true:支持 false:不支持
                iWndowType: 1,
                cbSelWnd: function (xmlDoc) {
                    $scope.g_iWndIndex = $(xmlDoc).find("SelectWnd").eq(0).text();
                    var szInfo = "当前选择的窗口编号：" + $scope.g_iWndIndex;
                    // showCBInfo(szInfo);
                }
            });
            WebVideoCtrl.I_InsertOBJECTPlugin("divPlugin");

            // 检查插件是否最新
            // if (-1 == WebVideoCtrl.I_CheckPluginVersion()) {
            //     alert("检测到新的插件版本，双击开发包目录里的WebComponentsKit.exe升级！");
            //     return;
            // }

            // 窗口事件绑定
            $(window).bind({
                resize: function () {
                    var $Restart = $("#restartDiv");
                    if ($Restart.length > 0) {
                        var oSize = getWindowSize();
                        $Restart.css({
                            width: oSize.width + "px",
                            height: oSize.height + "px"
                        });
                    }
                }
            });


            if ("" == szIP || "" == szPort) {
                return;
            }

            var iRet = WebVideoCtrl.I_Login(szIP, 1, szPort, szUsername, szPassword, {
                success: function (xmlDoc) {
                    // console.log(szIP + " 登录成功！");
                    console.log(szIP + " 登录成功！");

                    $scope.ipcStartRealPlay(szIP, false);
                },
                error: function () {
                    console.log(szIP + " 登录失败！");
                }
            });

            if (-1 == iRet) {
                console.log(szIP + " 已登录过！");
            }
        };


// 退出
        $scope.ipcLogout = function (camera) {
            var szIP = camera['ip'],
                szInfo = "";

            if (szIP == "") {
                return;
            }

            $scope.ipcStopRealPlay();

            var iRet = WebVideoCtrl.I_Logout(szIP);
            if (0 == iRet) {
                szInfo = "退出成功！";
                // getChannelInfo();
            } else {
                szInfo = "退出失败！";
            }
            console.log(szIP + " " + szInfo);
        };

// 默认都是IPC设备，都使用第一个模拟通道即可
        $scope.gIpcChn = 1;

// 开始预览
        $scope.ipcStartRealPlay = function (ip, isHd) {
            var oWndInfo = WebVideoCtrl.I_GetWindowStatus($scope.g_iWndIndex),
                szIP = ip,
                iStreamType = 2, // 子码流
                iChannelID = 1,
                bZeroChannel = false,
                szInfo = "";

            if (isHd) {
                iStreamType = 1;
            }

            if ("" == szIP) {
                return;
            }

            if (oWndInfo != null) {// 已经在播放了，先停止
                WebVideoCtrl.I_Stop();
            }

            var iRet = WebVideoCtrl.I_StartRealPlay(szIP, {
                iStreamType: iStreamType,
                iChannelID: iChannelID,
                bZeroChannel: bZeroChannel
            });

            if (0 == iRet) {
                szInfo = "开始预览成功！";
            } else {
                szInfo = "开始预览失败！";
            }

            console.log(szIP + " " + szInfo);
        };

// 停止预览
        $scope.ipcStopRealPlay = function () {
            var oWndInfo = WebVideoCtrl.I_GetWindowStatus($scope.g_iWndIndex),
                szInfo = "";

            if (oWndInfo != null) {
                var iRet = WebVideoCtrl.I_Stop();
                if (0 == iRet) {
                    szInfo = "停止预览成功！";
                } else {
                    szInfo = "停止预览失败！";
                }
                console.log(oWndInfo.szIP + " " + szInfo);
            }
        };


// PTZ控制 9为自动，1,2,3,4,5,6,7,8为方向PTZ
        $scope.g_bPTZAuto = false;
        $scope.mouseDownPTZControl = function (iPTZIndex) {
            var oWndInfo = WebVideoCtrl.I_GetWindowStatus($scope.g_iWndIndex),
                bZeroChannel = false,
                iPTZSpeed = 2;

            if (bZeroChannel) {// 零通道不支持云台
                return;
            }

            if (oWndInfo != null) {
                if (9 == iPTZIndex && $scope.g_bPTZAuto) {
                    iPTZSpeed = 0;// 自动开启后，速度置为0可以关闭自动
                } else {
                    $scope.g_bPTZAuto = false;// 点击其他方向，自动肯定会被关闭
                }

                WebVideoCtrl.I_PTZControl(iPTZIndex, false, {
                    iPTZSpeed: iPTZSpeed,
                    success: function (xmlDoc) {
                        if (9 == iPTZIndex) {
                            $scope.g_bPTZAuto = !$scope.g_bPTZAuto;
                        }
                        console.log(oWndInfo.szIP + " 开启云台成功！");
                    },
                    error: function () {
                        console.log(oWndInfo.szIP + " 开启云台失败！");
                    }
                });
            }
        };

// 方向PTZ停止
        $scope.mouseUpPTZControl = function () {
            var oWndInfo = WebVideoCtrl.I_GetWindowStatus($scope.g_iWndIndex);

            if (oWndInfo != null) {
                WebVideoCtrl.I_PTZControl(1, true, {
                    success: function (xmlDoc) {
                        console.log(oWndInfo.szIP + " 停止云台成功！");
                    },
                    error: function () {
                        console.log(oWndInfo.szIP + " 停止云台失败！");
                    }
                });
            }
        };



        $scope.PTZZoomIn = function () {
            var oWndInfo = WebVideoCtrl.I_GetWindowStatus($scope.g_iWndIndex);

            if (oWndInfo != null) {
                WebVideoCtrl.I_PTZControl(10, false, {
                    iWndIndex: $scope.g_iWndIndex,
                    success: function (xmlDoc) {
                        console.log(oWndInfo.szIP + " 调焦+成功！");
                    },
                    error: function () {
                        console.log(oWndInfo.szIP + "  调焦+失败！");
                    }
                });
            }
        };

        $scope.PTZZoomout = function () {
            var oWndInfo = WebVideoCtrl.I_GetWindowStatus($scope.g_iWndIndex);

            if (oWndInfo != null) {
                WebVideoCtrl.I_PTZControl(11, false, {
                    iWndIndex: $scope.g_iWndIndex,
                    success: function (xmlDoc) {
                        console.log(oWndInfo.szIP + " 调焦-成功！");
                    },
                    error: function () {
                        console.log(oWndInfo.szIP + "  调焦-失败！");
                    }
                });
            }
        };

        $scope.PTZZoomStop = function () {
            var oWndInfo = WebVideoCtrl.I_GetWindowStatus($scope.g_iWndIndex);

            if (oWndInfo != null) {
                WebVideoCtrl.I_PTZControl(11, true, {
                    iWndIndex: $scope.g_iWndIndex,
                    success: function (xmlDoc) {
                        console.log(oWndInfo.szIP + " 调焦停止成功！");
                    },
                    error: function () {
                        console.log(oWndInfo.szIP + "  调焦停止失败！");
                    }
                });
            }
        };

        /**
         * 切换高清流
         */
        $scope.changeHDView = function (isHd) {
            $scope.ipcStopRealPlay();

            $scope.ipcStartRealPlay($scope.gSzIp, isHd);
        }
    }

})();
